<template>
  <div class="container">
    <div class="navList">
      <router-link to="/userList">
        <span class="spanUser">用户管理</span>
        <span class="spanArrow">></span>
      </router-link>
      <span class="spanAdd">新增用户</span>
    </div>

    <section class="addUserContent">
      <div class="addUserForm">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="账号名称" prop="accountName" :rules="[{required: true, message: '请输入用户名称', trigger: 'blur'}]">
            <el-input v-model="form.accountName" v-on:blur="judgeAccountName" size="small"></el-input>
          </el-form-item>

          <el-form-item label="真实姓名" prop="userName" :rules="[{required: true, message: '请输入真实姓名', trigger: 'blur'}]">
            <el-input v-model="form.userName" size="small"></el-input>
          </el-form-item>

          <el-form-item label="性别" prop="sex">
            <el-select v-model="form.sex" size="small" placeholder="请选择性别">
              <el-option label="男" value="M"></el-option>
              <el-option label="女" value="F"></el-option>
              <el-option label="其他" value="O"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="生日" prop="birthday">
            <el-date-picker type="date" size="small" placeholder="请选择日期" v-model="form.birthday" style="width: 100%"></el-date-picker>
          </el-form-item>

          <el-form-item label="电话号码" prop="telephone" :rules="[{required: false, pattern: /^1[34578]\d{9}$|^([0-9]{3,4}-)?[0-9]{7,8}$/, message: '请输入正确格式', trigger: 'change'}]">
            <el-input v-model="form.telephone" size="small"></el-input>
          </el-form-item>

          <el-form-item label="初始密码" prop="accountPwd">
            <el-input v-model="form.accountPwd" size="small" :disabled="true"></el-input>
          </el-form-item>

          <el-form-item label="所属机构" prop="institutionName" :rules="[{required: true, message: '请选择所属机构', trigger: 'change'}]">
            <el-select v-model="form.institutionName" size="small" filterable placeholder="请选择所属机构">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <!--  <el-form-item
            label="用户角色"
            prop="userRules"
            :rules="[{type: 'array', required: true, message: '请至少选择一个角色', trigger: 'change'}]"
            >
            <el-checkbox-group 
              v-model="form.userRules"
              :min="1"
              :max="7" size="small">
              <el-checkbox v-for="rule in rules" :label="rule.value" :key="rule.value">{{rule.label}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item> -->

          <el-form-item label="职位信息" prop="position" :rules="[{required: true, message: '请输入职位信息', trigger: 'blur'}]">
            <el-input v-model="form.position" size="small"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button size="small" type="primary" @click.native.prevent="addUser">保存</el-button>
            <el-button size="small" @click.native.prevent="goBack">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </section>

  </div>
</template>

<script>
import util from "../../common/util";
import { addUser, judgeUserName } from "../../api/api";
export default {
  data() {
    return {
      form: {
        accountName: "",
        userName: "",
        sex: "",
        birthday: "",
        telephone: "",
        accountPwd: "123456",
        institutionName: "",
        //userRules: [],
        position: ""
      },
      options: [
        {
          value: "1",
          label: "北京"
        },
        {
          value: "2",
          label: "上海"
        },
        {
          value: "3",
          label: "广州"
        },
        {
          value: "4",
          label: "深圳"
        },
        {
          value: "5",
          label: "杭州"
        }
      ],
      rules: [
        {
          value: "1",
          label: "客服"
        },
        {
          value: "2",
          label: "技师"
        },
        {
          value: "3",
          label: "分片医生"
        },
        {
          value: "4",
          label: "初级报告医生"
        },
        {
          value: "5",
          label: "初级审核医生"
        },
        {
          value: "6",
          label: "高级报告医生"
        },
        {
          value: "7",
          label: "高级审核医生"
        }
      ]
    };
  },
  methods: {
    judgeAccountName() {
      //判断该用户名是否已存在
      var _this = this;
      //表单验证和用户名存在情况处理待完善
      axios
        .get(
          "/account/queryAccountByAccountName/?accountName=" +
            _this.form.accountName
        )
        .then(function(res) {
          if (res.data.status !== "200") {
            _this.$message({
              type: "error",
              message: "用户名已存在"
            });
          }
        });
    },
    addUser() {
      var _this = this;
      this.$refs.form.validate(function(valid) {
        if (valid) {
          //保存处理
          let para = Object.assign({}, _this.form);
          para.birthday =
            !para.birthday || para.birthday == ""
              ? ""
              : util.formatDate.format(new Date(para.birthday), "yyyy-MM-dd");
          axios
            .get(
              "/account/queryAccountByAccountName/?accountName=" +
                _this.form.accountName
            )
            .then(function(res) {
              if (res.data.status === "200") {
                addUser(para).then(function(res) {
                  if (res.data.status === "200") {
                    _this.$router.push({ path: "/userList" });
                  }
                });
              } else {
                _this.$message({
                  type: "error",
                  message: "用户名已存在,请重新填写"
                });
              }
            });
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$router.push({ path: "/userList" });
    }
  }
};
</script>

<style lang="scss" scoped>
.navList {
  height: 55px;
  text-align: left;
  border-bottom: 4px solid #cdd1da;
  line-height: 55px;
  padding-left: 20px;

  .spanUser {
    color: #333;
    font-size: 22px;
  }
  .spanArrow {
    color: #333;
    font-size: 16px;
    padding-left: 5px;
  }
  .spanAdd {
    font-size: 15px;
  }
  a {
    text-decoration: none;
  }
  .spanUser:hover {
    color: #3c8dbc;
  }
}

.addUserContent {
  width: 100%;
  height: 650px;
  background: #fff;
  padding-top: 10px;
  .el-form-item {
    margin-bottom: 17px;
  }
}
.addUserForm {
  width: 540px;
  padding: 5px 10px 10px;
  margin-left: 190px;
  border: solid 3px #eaeaea;
  border-radius: 5px;
  box-shadow: 0px 0px 10px #dcdcdc;
}
.addUserForm .el-select {
  display: block;
}
</style>